﻿
@{
    ViewBag.Title = "FormBusiness";
    Layout = "~/Views/Shared/layoutCommon.cshtml";
}

<section class="content-header">
    <h1>
        @ViewBag.CityName 配置业务
        <small></small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i>主页</a></li>
        <li><a href="/config/CityOpenManage?provinceid=@ViewBag.ProvinceId">省市业务开通</a></li>
        <li class="active">配置业务</li>
    </ol>
</section>
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="box box-primary">
                <div class="box-header">
                    <div class="form-inline">
                        <div class="form-group">
                            <label>   </label>
                            <input type="checkbox" id="btnAll" /> 全选
                        </div>
                        <div class="form-group" style="margin-left:10px;">
                            <label>   </label>
                            <button type="button" class="btn btn-info btn-sm" id="binSave">开启选中</button>
                        </div>
                        <div class="form-group" style="margin-left:10px;">
                            <label>   </label>
                            <button type="button" class="btn btn-danger btn-sm" id="btnCancel">关闭选中</button>
                        </div>
                    </div>
                </div>
                <!-- /.box-header -->
                <div class="box-body table-responsive no-padding">
                    <table class="table table-hover body-table-head">
                        <thead>
                            <tr>
                                <th class="t_c">
                                    选择
                                </th>
                                <th>
                                    业务ID
                                </th>
                                <th>
                                    业务名称
                                </th>
                                <th>
                                    所属标签
                                </th>
                                <th class="t_c">
                                    状态
                                </th>
                                <th class="t_c">
                                    创建时间
                                </th>
                                <th class="t_c">
                                    操作
                                </th>
                            </tr>
                        </thead>
                        <tbody id="tbodylist"></tbody>
                    </table>
                </div>
                <div class="no-data" id="nodata" style="display:none;">
                    <div class="img-box"></div>
                    <div class="text">暂无数据</div>
                </div>
                <div class="box-footer clearfix ">
                    <ul id="pagination" class="pagination pagination-sm no-margin pull-right"></ul>
                </div>
                <!-- /.box-body -->
            </div>
            <!-- /.box -->
        </div>

    </div>
</section>
@section scriptsBottom{
    <script src="@Url.Content("~/lib/art-template/template-web.js")"></script>
    <script id="dataItemTpl" type="text/html">
        {{each data}}
        <tr>
            <td class="t_c">
                <input type="checkbox" value="{{$value.BizTypeID}}" />
            </td>
            <td>
                {{$value.BizTypeID}}
            </td>
            <td>
                {{$value.BizTypeName}}
            </td>
            <td>
                {{$value.MasterBizTypeName}}
            </td>
            <td class="t_c">
                {{if $value.Status==1}}
                已开启
                {{else}}
                已关闭
                {{/if}}
            </td>
            <td class="t_c">
                {{if $value.CreatedDate!='0001-01-01 00:00:00'}}
                {{$value.CreatedDate}}
                {{/if}}
            </td>
            <td class="t_c">
                <button type="button" class="btn btn-warning btn-xs" onclick="config('{{$value.BizTypeCityRelaID}}')">配置表单</button>
            </td>
        </tr>
        {{/each}}
    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            search();
            $("#btnAll").click(function(){
                var $thisCheck = false;
                if ($(this).prop("checked") == true) {
                    $(this).prop("checked", true);
                    $thisCheck = true;
                } else {
                    $(this).prop("checked", false);
                }
                $("#tbodylist").find("input:checkbox").each(function(){
                    if ($thisCheck) {
                        $(this).prop("checked", true);
                    } else {
                        $(this).prop("checked", false);
                    }
                })
            });

            $("#binSave").click(function(){
                save();
            });
            $("#btnCancel").click(function(){
                cancel();
            });
        });

        function bindCheckBox()
        {
            var chblen = $("#tbodylist").find("input:checkbox").length;
            $("#tbodylist").find("input:checkbox").each(function(){
                $(this).click(function(){
                    var $thisCheck = false;
                    if ($(this).prop("checked") == true) {
                        $(this).prop("checked", true);
                        $thisCheck = true;
                    } else {
                        $(this).prop("checked", false);
                    }
                    if($("#tbodylist").find("[type=checkbox]:checked").length==chblen)
                    {
                        $("#btnAll").prop("checked", true);
                    }else
                    {
                        $("#btnAll").prop("checked", false);
                    }

                });
            });
        }

        //页码
        var pageIndex = 1;
        //查询
        function search() {
            //查询列表
            $.ajax({
                url: "@Url.Content("~/Config/GetFormFieldCfgList")",
                type: 'GET',
                dataType: "json",
                data: {
                    CityId: @ViewBag.CityId,
                    rows: 10,
                    page: pageIndex
                },
                success: function (data) {
                    if (data.data.length < 1) {
                        $("#nodata").show();
                        document.getElementById('tbodylist').innerHTML = "";
                        return;
                    }
                    $("#nodata").hide();
                    //模板渲染
                    var data = data;
                    var html = template('dataItemTpl', data);
                    document.getElementById('tbodylist').innerHTML = html;
                    bindCheckBox();
                    //分页显示
                    $('#pagination').twbsPagination("destroy");
                    $('#pagination').twbsPagination({
                        totalItems: parseInt(data.message),
                        visiblePages: 10,
                        pageSize: 10,
                        startPage: parseInt(pageIndex),
                        onPageClick: function (event, page) {
                            pageIndex = page;
                            search();
                        }
                    });
                }
            });
        }

        function save()
        {
            var chklist = checkData();
            if(chklist=="")
            {
                BMCW.alert("请选择业务");
                return;
            }
            BMCW.ajax({
                url: "@Url.Content("~/Config/SaveFormFieldCfg")",
                type: "Post",
                data: {
                    CityId: @ViewBag.CityId,
                    BizTypeIds: chklist
                },
                success: function (o) {
                    if (o.status == 0&&o.data==true) {
                        BMCW.msg("开启成功",function(){
                            search();
                        });
                    } else {
                        BMCW.alert("开启失败");
                    }
                }
            });
        }

        function cancel()
        {
            var chklist = checkData();
            if(chklist=="")
            {
                BMCW.alert("请选择业务");
                return;
            }
            if(chklist==null)return;
            BMCW.DataConfirm("确认要关闭选中配置吗?", function () {
                BMCW.ajax({
                    url: "@Url.Content("~/Config/CancelFormFieldCfg")",
                    type: "Post",
                    data: {
                        CityId: @ViewBag.CityId,
                        BizTypeIds: chklist
                    },
                    success: function (o) {
                        if (o.status == 0&&o.data==true) {
                            BMCW.msg("关闭成功",function(){
                                search();
                            });
                        } else {
                            BMCW.alert("关闭失败");
                        }
                    }
                });
            });
        }

        function checkData()
        {
            var $checkedlist="";
            var $selectList = $("#tbodylist");
            $selectList.find("input:checkbox").each(function () {
                var $this = $(this);
                if ($this.prop("checked") == true) {
                    if ($checkedlist == "") {
                        $checkedlist += $this.val();
                    } else {
                        $checkedlist += "," + $this.val();
                    }
                }
            });
           
            return $checkedlist;
        }

        //跳转配置表单
        function config(cgfid)
        {
            if(cgfid==0)
            {
                BMCW.alert("此业务未开启,不能配置表单");
                return;
            }
            location.href="@Url.Content("~/Config/ConfigForm")?provinceid=@ViewBag.ProvinceId&cityname=@ViewBag.CityName&cgfid=" + cgfid;
            }

    </script>
}

